<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper 节点类型详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9f9f9;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
        }
        .highlight-box {
            border-left: 4px solid #667eea;
            background-color: rgba(102, 126, 234, 0.05);
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background-color: rgba(102, 126, 234, 0.1);
            color: #667eea;
            font-size: 24px;
            margin-bottom: 20px;
        }
        .node-type-card {
            transition: all 0.3s ease;
            border-top: 3px solid transparent;
        }
        .node-type-card:hover {
            border-top-color: #667eea;
        }
        .ephemeral-card {
            border-color: #f687b3;
        }
        .ephemeral-card:hover {
            border-top-color: #f687b3;
        }
        .persistent-card {
            border-color: #667eea;
        }
        .persistent-card:hover {
            border-top-color: #667eea;
        }
        .footer-link {
            transition: all 0.2s ease;
        }
        .footer-link:hover {
            color: #fff !important;
            text-decoration: underline;
        }
        .mermaid-container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="bg-white shadow-sm py-4">
        <div class="container mx-auto px-4 md:px-6 flex justify-between items-center">
            <a href="#" class="text-xl font-bold text-gray-800 serif flex items-center">
                <i class="fas fa-network-wired text-indigo-600 mr-2"></i>
                <span>技术小馆</span>
            </a>
            <div class="hidden md:flex space-x-8">
                <a href="#" class="text-gray-600 hover:text-indigo-600 transition">首页</a>
                <a href="#" class="text-gray-600 hover:text-indigo-600 transition">分布式系统</a>
                <a href="#" class="text-gray-600 hover:text-indigo-600 transition">Zookeeper</a>
                <a href="#" class="text-gray-600 hover:text-indigo-600 transition">技术专栏</a>
            </div>
            <button class="md:hidden text-gray-600">
                <i class="fas fa-bars"></i>
            </button>
        </div>
    </nav>

    <!-- Hero 区域 -->
    <header class="hero-gradient text-white py-20 md:py-32">
        <div class="container mx-auto px-4 md:px-6 text-center">
            <div class="max-w-3xl mx-auto">
                <span class="inline-block bg-white text-indigo-600 text-sm font-semibold px-3 py-1 rounded-full mb-4">Zookeeper 专题</span>
                <h1 class="text-3xl md:text-5xl font-bold mb-6 serif">深入理解 Zookeeper 节点类型</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">临时节点与持久节点的特性、区别及应用场景全面解析</p>
                <div class="flex justify-center space-x-4">
                    <a href="#content" class="bg-white text-indigo-600 hover:bg-gray-100 font-medium px-6 py-3 rounded-full transition">
                        <i class="fas fa-book-open mr-2"></i>开始阅读
                    </a>
                    <a href="#visualization" class="bg-indigo-800 text-white hover:bg-indigo-900 font-medium px-6 py-3 rounded-full transition">
                        <i class="fas fa-project-diagram mr-2"></i>查看图表
                    </a>
                </div>
            </div>
        </div>
    </header>

    <!-- 主要内容 -->
    <main class="container mx-auto px-4 md:px-6 py-12 md:py-20">
        <!-- 介绍部分 -->
        <section class="max-w-4xl mx-auto mb-16">
            <div class="text-center mb-12">
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 mb-4 serif">Zookeeper 节点类型解析</h2>
                <div class="w-24 h-1 bg-indigo-600 mx-auto"></div>
            </div>
            
            <p class="text-gray-700 mb-6 text-lg leading-relaxed">
                Zookeeper 作为分布式系统的协调服务，其数据模型基于节点（ZNode）。节点分为两种主要类型：<span class="font-bold text-indigo-600">临时节点（Ephemeral Nodes）</span>和<span class="font-bold text-indigo-600">持久节点（Persistent Nodes）</span>。理解这两种节点的区别对于设计可靠的分布式系统至关重要。
            </p>
            
            <div class="highlight-box p-6 rounded-r-lg mb-8">
                <h3 class="text-lg font-bold text-gray-800 mb-3 flex items-center">
                    <i class="fas fa-lightbulb text-indigo-600 mr-2"></i>关键区别
                </h3>
                <p class="text-gray-700">
                    临时节点的生命周期与客户端会话绑定，会话结束则节点自动删除；持久节点则独立于客户端会话，需要显式删除。这一根本区别决定了它们各自的应用场景。
                </p>
            </div>
        </section>

        <!-- 节点类型对比 -->
        <section class="mb-20">
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 临时节点卡片 -->
                <div class="bg-white rounded-lg shadow-md p-8 node-type-card ephemeral-card">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-center text-pink-600 mb-4 serif">临时节点</h3>
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-hourglass-end text-pink-500 mr-2"></i>生命周期
                    </h4>
                    <p class="text-gray-700 mb-4">
                        与客户端的 Session 相关联。当客户端断开连接（如崩溃或网络故障）时，Zookeeper 会自动删除这些节点。
                    </p>
                    
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-magic text-pink-500 mr-2"></i>特性
                    </h4>
                    <ul class="text-gray-700 mb-6 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-pink-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>客户端会话结束时自动删除</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-pink-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>不能有子节点（在Zookeeper 3.5.0之后允许临时节点的子节点）</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-pink-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>适用于临时状态表示</span>
                        </li>
                    </ul>
                    
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-laptop-code text-pink-500 mr-2"></i>使用场景
                    </h4>
                    <div class="grid grid-cols-1 gap-3">
                        <div class="bg-pink-50 border border-pink-100 rounded-lg p-3">
                            <h5 class="font-medium text-pink-700 mb-1">客户端状态指示</h5>
                            <p class="text-sm text-gray-700">表示客户端在服务中的活跃状态，断开连接自动清除</p>
                        </div>
                        <div class="bg-pink-50 border border-pink-100 rounded-lg p-3">
                            <h5 class="font-medium text-pink-700 mb-1">分布式锁</h5>
                            <p class="text-sm text-gray-700">作为锁的持有者标识，断开连接自动释放锁</p>
                        </div>
                        <div class="bg-pink-50 border border-pink-100 rounded-lg p-3">
                            <h5 class="font-medium text-pink-700 mb-1">临时数据存储</h5>
                            <p class="text-sm text-gray-700">会话期间需要的临时数据，会话结束自动清理</p>
                        </div>
                    </div>
                </div>
                
                <!-- 持久节点卡片 -->
                <div class="bg-white rounded-lg shadow-md p-8 node-type-card persistent-card">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-anchor"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-center text-indigo-600 mb-4 serif">持久节点</h3>
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-infinity text-indigo-500 mr-2"></i>生命周期
                    </h4>
                    <p class="text-gray-700 mb-4">
                        独立于客户端 Session。一旦创建，会一直存在，直到显式删除。客户端断开连接不会影响节点存在。
                    </p>
                    
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-magic text-indigo-500 mr-2"></i>特性
                    </h4>
                    <ul class="text-gray-700 mb-6 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>需要显式调用删除操作</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>可以有子节点</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2 flex-shrink-0"></i>
                            <span>适用于持久化数据存储</span>
                        </li>
                    </ul>
                    
                    <h4 class="text-lg font-semibold text-gray-800 mb-2 flex items-center">
                        <i class="fas fa-laptop-code text-indigo-500 mr-2"></i>使用场景
                    </h4>
                    <div class="grid grid-cols-1 gap-3">
                        <div class="bg-indigo-50 border border-indigo-100 rounded-lg p-3">
                            <h5 class="font-medium text-indigo-700 mb-1">配置存储</h5>
                            <p class="text-sm text-gray-700">存储系统配置数据，需要在系统重启后保持</p>
                        </div>
                        <div class="bg-indigo-50 border border-indigo-100 rounded-lg p-3">
                            <h5 class="font-medium text-indigo-700 mb-1">持久状态</h5>
                            <p class="text-sm text-gray-700">保存需要长期存在的系统状态信息</p>
                        </div>
                        <div class="bg-indigo-50 border border-indigo-100 rounded-lg p-3">
                            <h5 class="font-medium text-indigo-700 mb-1">节点层次结构</h5>
                            <p class="text-sm text-gray-700">构建长期有效的目录结构和服务目录</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 对比表格 -->
        <section class="mb-20" id="content">
            <div class="bg-white rounded-lg shadow-md overflow-hidden">
                <div class="p-6 border-b">
                    <h3 class="text-xl font-bold text-gray-800 flex items-center">
                        <i class="fas fa-table text-indigo-600 mr-2"></i>
                        <span class="serif">临时节点与持久节点对比表</span>
                    </h3>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-pink-500 uppercase tracking-wider">临时节点</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-indigo-500 uppercase tracking-wider">持久节点</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">生命周期</td>
                                <td class="px-6 py-4 whitespace-nowrap text-pink-600">与客户端会话绑定</td>
                                <td class="px-6 py-4 whitespace-nowrap text-indigo-600">独立于客户端会话</td>
                            </tr>
                            <tr class="bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">删除时机</td>
                                <td class="px-6 py-4 whitespace-nowrap text-pink-600">会话结束自动删除</td>
                                <td class="px-6 py-4 whitespace-nowrap text-indigo-600">需要显式删除</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">子节点</td>
                                <td class="px-6 py-4 whitespace-nowrap text-pink-600">传统上不能有子节点</td>
                                <td class="px-6 py-4 whitespace-nowrap text-indigo-600">可以有子节点</td>
                            </tr>
                            <tr class="bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">适用场景</td>
                                <td class="px-6 py-4 whitespace-nowrap text-pink-600">临时状态、分布式锁</td>
                                <td class="px-6 py-4 whitespace-nowrap text-indigo-600">配置存储、持久状态</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">数据持久性</td>
                                <td class="px-6 py-4 whitespace-nowrap text-pink-600">低</td>
                                <td class="px-6 py-4 whitespace-nowrap text-indigo-600">高</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>

        <!-- 可视化图表 -->
        <section class="mb-20" id="visualization">
            <div class="text-center mb-12">
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 mb-4 serif">Zookeeper 节点类型关系图</h2>
                <div class="w-24 h-1 bg-indigo-600 mx-auto"></div>
            </div>
            
            <div class="mermaid-container">
                <div class="mermaid">
                    graph TD
                    A[Zookeeper Nodes] --> B[临时节点]
                    A --> C[持久节点]
                    
                    B --> D[生命周期]
                    D --> D1[与客户端会话绑定]
                    D --> D2[会话结束自动删除]
                    
                    B --> E[使用场景]
                    E --> E1[分布式锁]
                    E --> E2[客户端状态指示]
                    E --> E3[临时数据存储]
                    
                    C --> F[生命周期]
                    F --> F1[独立于客户端会话]
                    F --> F2[需要显式删除]
                    
                    C --> G[使用场景]
                    G --> G1[配置存储]
                    G --> G2[持久状态]
                    G --> G3[节点层次结构]
                    
                    style A fill:#f0f9ff,stroke:#333,stroke-width:2px
                    style B fill:#fff5f7,stroke:#f687b3,stroke-width:2px
                    style C fill:#f0f5ff,stroke:#667eea,stroke-width:2px
                    
                    classDef default font-family:'Noto Sans SC',sans-serif
                </div>
            </div>
        </section>

        <!-- 应用示例 -->
        <section class="mb-20">
            <div class="text-center mb-12">
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800 mb-4 serif">实际应用示例</h2>
                <div class="w-24 h-1 bg-indigo-600 mx-auto"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-lg shadow-md overflow-hidden card-hover">
                    <div class="p-6 bg-indigo-600 text-white">
                        <h3 class="text-xl font-bold flex items-center">
                            <i class="fas fa-lock-open mr-2"></i>
                            分布式锁实现
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">
                            使用临时节点实现分布式锁是Zookeeper的典型应用。当客户端尝试获取锁时，它会在指定路径下创建一个临时顺序节点。
                        </p>
                        <div class="bg-gray-50 p-4 rounded-md mb-4">
                            <h4 class="font-medium text-gray-800 mb-2">实现步骤:</h4>
                            <ol class="list-decimal list-inside text-gray-700 space-y-1">
                                <li>客户端在锁路径下创建临时顺序节点</li>
                                <li>检查是否是最小序号的节点</li>
                                <li>如果是，获取锁；否则监听前一个节点</li>
                                <li>前一个节点删除时，重新检查序号</li>
                            </ol>
                        </div>
                        <p class="text-gray-700">
                            这种实现方式确保当客户端崩溃时，其创建的临时节点会被自动删除，从而释放锁资源，避免了死锁问题。
                        </p>
                    </div>
                </div>
                
                <div class="bg-white rounded-lg shadow-md overflow-hidden card-hover">
                    <div class="p-6 bg-pink-600 text-white">
                        <h3 class="text-xl font-bold flex items-center">
                            <i class="fas fa-server mr-2"></i>
                            服务发现系统
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">
                            结合持久节点和临时节点可以实现高效的服务发现系统。持久节点用于存储服务配置，临时节点用于表示服务实例状态。
                        </p>
                        <div class="bg-gray-50 p-4 rounded-md mb-4">
                            <h4 class="font-medium text-gray-800 mb-2">节点结构:</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>/services (持久节点)</li>
                                <li>/services/serviceA (持久节点，存储配置)</li>
                                <li>/services/serviceA/instance1 (临时节点，表示实例)</li>
                                <li>/services/serviceA/instance2 (临时节点，表示实例)</li>
                            </ul>
                        </div>
                        <p class="text-gray-700">
                            当服务实例下线或崩溃时，其临时节点会自动删除，客户端可以实时感知到服务实例的变化。
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="bg-gray-900 text-gray-400 py-12">
        <div class="container mx-auto px-4 md:px-6">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2 flex items-center">
                        <i class="fas fa-network-wired text-indigo-400 mr-2"></i>
                        技术小馆
                    </h3>
                    <p class="text-sm">探索分布式系统的核心技术</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="footer-link text-gray-400 hover:text-white">
                        <i class="fas fa-external-link-alt mr-1"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <!-- Mermaid 图表库 -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        // 初始化 Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            fontFamily: "'Noto Sans SC', sans-serif"
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>